package com.ms.rc_lab;

import android.util.Log;

import java.util.Vector;

public class CommandCenter {
    /**
     * 命令格式： 目标 + 命令|通道 + value
     * 对于小车来说： car _ forward _ 50 : 以50的速度往前走
     * 对于pwm舵机来说： pwm _ 1 _ -30: pwm 通道一旋转-30度
     */
    //8个方向定义
    public static String CMD_FORWARD = "ff";  // 前
    public static String CMD_BACKWARD = "bb"; // 后
    public static String CMD_LEFT = "ll";  // 左
    public static String CMD_RIGHT = "rr"; // 右

    public static String CMD_RIGHT_FORWARD = "rf"; //右前
    public static String CMD_LEFT_FORWARD = "lf";// 左后
    public static String CMD_RIGHT_BACKWARD = "rb";// 右后
    public static String CMD_LEFT_BACKWARD = "lb"; // 左后

    public static String CMD_CENTER = "cc";  // 中心


    //旋转定义
    public static String ROTATE_CLOCKWISE = "lr";  // 顺时针旋转
    public static String ROTATE_COUNTERCLOCKWISE = "rl";  // 逆时针旋转

    //操作体定义
    public static String CAR = "car";
    public static String PWM = "pwm";
    public static String MOT = "mot";

    // 定义8个PWM，玩家可以在这里随意更改映射关系。
    public static int PWM_C1 = 0;
    public static int PWM_C2 = 1;
    public static int PWM_C3 = 2;
    public static int PWM_C4 = 3;
    public static int PWM_C5 = 4;
    public static int PWM_C6 = 5;
    public static int PWM_C7 = 6;
    public static int PWM_C8 = 7;

    static char crcValide(String sd, int off){
        byte[] data = sd.getBytes();
        byte checksum = 0;
        for (int i = off; i < data.length; i++) {
            checksum ^= data[i];
        }
        char c = (char) (checksum & 0xFF);
        return c;
    }
    public static String commandCat(String des, String channels, int speed){
        StringBuilder builder = new StringBuilder();
        builder.append('#');
        builder.append(des);
        builder.append('_');
        builder.append(channels);
        builder.append('_');
        builder.append(speed);
        builder.append(crcValide(builder.toString(), 1));
        builder.append('\n');
        Log.d("test", builder.toString());
        return builder.toString();
    }

    public static String getDirection(RockerView.Direction direction){
        switch (direction){
            case DIRECTION_UP: {
                return CMD_FORWARD;
            }
            case DIRECTION_DOWN:{
                return CMD_BACKWARD;
            }
            case DIRECTION_LEFT:{
                return CMD_LEFT;
            }
            case DIRECTION_RIGHT:{
                return CMD_RIGHT;
            }
            case DIRECTION_DOWN_LEFT:{
                return CMD_LEFT_BACKWARD;
            }
            case DIRECTION_DOWN_RIGHT:{
                return CMD_RIGHT_BACKWARD;
            }
            case DIRECTION_UP_LEFT:{
                return CMD_LEFT_FORWARD;
            }
            case DIRECTION_UP_RIGHT:{
                return CMD_RIGHT_FORWARD;
            }
            case DIRECTION_CENTER:{
                return CMD_CENTER;
            }
            default:{
                Log.e("COMMAND", "command not supported error, Direction is: "+direction);
                return "";
            }
        }
    }

    public static String getPWMChannel(RockerView.Direction direction){
        switch (direction){
            case DIRECTION_UP:
            case DIRECTION_DOWN: {
                return String.valueOf(PWM_C1);
            }
            case DIRECTION_LEFT:
            case DIRECTION_RIGHT: {
                return String.valueOf(PWM_C2);
            }
            case DIRECTION_CENTER:
                return "";
            default:{
                Log.e("COMMAND", "command not supported error, Direction is: "+direction);
                return "";
            }
        }
    }
}
